<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="工单号" prop="po" label-width="88px">
                <el-input v-model="queryParams.po" placeholder="请输入工单号" clearable size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="产品条码" prop="barCode" label-width="88px">
                <el-input v-model="queryParams.barCode" placeholder="请输入产品条码" clearable size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="物料编码" prop="itemCode">
                <el-input v-model="queryParams.itemCode" placeholder="请输入物料编码" clearable size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>

            <el-form-item label="上传标识" prop="uploadFlag" label-width="88px">
                <el-input v-model="queryParams.uploadFlag" placeholder="请输入上传标识" clearable size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>


            <el-form-item label="物料09码" prop="segment8" label-width="88px">
                <el-input v-model="queryParams.segment8" placeholder="请输入物料09码" clearable size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>

            <el-form-item label="华为任务令" prop="segment4" label-width="88px">
                <el-input v-model="queryParams.segment4" placeholder="请输入华为任务令" clearable size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="产品编码" prop="barItemCode">
                <el-input v-model="queryParams.barItemCode" placeholder="请输入产品编码" clearable size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>

            <el-form-item label="物料PSN码" prop="segment13" label-width="88px">
                <el-input v-model="queryParams.segment13" placeholder="请输入物料PSN码" clearable size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>

            <el-form-item label="采集时间" prop="createdDate" label-width="88px">
                <el-date-picker v-model="dateRange" type="daterange" range-separator="至" start-placeholder="开始日期"
                    end-placeholder="结束日期" value-format="yyyy-MM-dd HH:mm:ss" :default-time="['00:00:00', '23:59:59']">
                </el-date-picker>
            </el-form-item>
            <el-form-item>
                <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="barcodeMaterialList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="工单号" align="center" prop="po" />
            <el-table-column label="产品条码" align="center" prop="barCode" />
            <el-table-column label="物料编码" align="center" prop="itemCode" />
            <el-table-column label="物料制造商" align="center" prop="manufactory" width="120" />
            <el-table-column label="物料制造周期" align="center" prop="dateCode" width="120" />
            <el-table-column label="物料制造批次" align="center" prop="lotCode" width="120" />
            <el-table-column label="bom数量" align="center" prop="quantity" />
            <el-table-column label="物料供应商" align="center" prop="supplier" width="120" />
            <el-table-column label="供应商编码" align="center" prop="vendorId" width="120" />
            <el-table-column label="orgId" align="center" prop="orgId" />
            <el-table-column label="条码采集时间" align="center" prop="createdDate" width="180" />
            <el-table-column label="加工厂名称" align="center" prop="createdBy" width="120" />
            <el-table-column label="上传标识" align="center" prop="uploadFlag">
                <template slot-scope="scope">
                    <span>{{
                        scope.row.uploadFlag===0?'待上载':scope.row.uploadFlag===1?'正在上载':scope.row.uploadFlag===2?'上载成功':scope.row.uploadFlag===9?'上载失败':''
                        }}</span>
                </template>
            </el-table-column>
            <el-table-column label="物料09码" align="center" prop="segment1" />
            <el-table-column label="segment2" align="center" prop="segment2" width="100" />
            <el-table-column label="segment3" align="center" prop="segment3" width="100" />
            <el-table-column label="华为任务令" align="center" prop="segment4" width="100" />/>
            <el-table-column label="emsTransId" align="center" prop="emsTransId" width="100" />
            <el-table-column label="产品编码" align="center" prop="barItemCode" width="100" />
            <el-table-column label="segment8" align="center" prop="segment8" width="100" />
            <el-table-column label="segment13" align="center" prop="segment13" width="100" />
            <el-table-column label="segment14" align="center" prop="segment14" width="100" />
            <el-table-column label="segment15" align="center" prop="segment15" width="100" />
            <el-table-column label="物料编码前两位" align="center" prop="ptionSegment" width="120" />
        </el-table>
        <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />
    </div>
</template>

<script>
    import { listBarcodeMaterial, getBarcodeMaterial, delBarcodeMaterial, addBarcodeMaterial, updateBarcodeMaterial, exportBarcodeMaterial } from "@/api/ems/barcodeMaterial";

    export default {
        name: "BarcodeMaterial",
        data() {
            return {
                // 遮罩层
                loading: true, dateRange: [],
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // 批次物料产品关系追溯表格数据
                barcodeMaterialList: [],
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    po: null,
                    barCode: null,
                    itemCode: null,
                    manufactory: null,
                    dateCode: null,
                    lotCode: null,
                    quantity: null,
                    supplier: null,
                    vendorId: null,
                    orgId: null,
                    createdDate: null,
                    createdBy: null,
                    lastUpdatedDate: null,
                    lastUpdatedBy: null,
                    uploadFlag: null,
                    checkMessage: null,
                    segment1: null,
                    segment2: null,
                    segment3: null,
                    segment4: null,
                    segment5: null,
                    segment6: null,
                    segment7: null,
                    emsTransId: null,
                    itemId: null,
                    checkFlag: null,
                    checkInfo: null,
                    barItemCode: null,
                    barItemId: null,
                    segment8: null,
                    segment9: null,
                    segment10: null,
                    segment11: null,
                    segment12: null,
                    segment13: null,
                    segment14: null,
                    segment15: null,
                    ptionSegment: null
                },
                // 表单参数
                form: {},
                // 表单校验
                rules: {
                }
            };
        },
        created() {
            this.getList();
        },
        methods: {
            /** 查询批次物料产品关系追溯列表 */
            getList() {
                this.loading = true;
                listBarcodeMaterial(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
                    this.barcodeMaterialList = response.rows;
                    this.total = response.total;
                    this.loading = false;
                });
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form = {};
                this.resetForm("form");
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.dateRange = []
                this.resetForm("queryForm");
                this.handleQuery();
            },
            // 多选框选中数据
            handleSelectionChange(selection) {
                this.ids = selection.map(item => item.po)
                this.single = selection.length !== 1
                this.multiple = !selection.length
            },
            /** 新增按钮操作 */
            handleAdd() {
                this.reset();
                this.open = true;
                this.title = "添加批次物料产品关系追溯";
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset();
                const po = row.po || this.ids
                getBarcodeMaterial(po).then(response => {
                    this.form = response.data;
                    this.open = true;
                    this.title = "修改批次物料产品关系追溯";
                });
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        if (this.form.po != null) {
                            updateBarcodeMaterial(this.form).then(response => {
                                if (response.code === 200) {
                                    this.msgSuccess("修改成功");
                                    this.open = false;
                                    this.getList();
                                }
                            });
                        } else {
                            addBarcodeMaterial(this.form).then(response => {
                                if (response.code === 200) {
                                    this.msgSuccess("新增成功");
                                    this.open = false;
                                    this.getList();
                                }
                            });
                        }
                    }
                });
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                const pos = row.po || this.ids;
                this.$confirm('是否确认删除批次物料产品关系追溯编号为"' + pos + '"的数据项?', "警告", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(function () {
                    return delBarcodeMaterial(pos);
                }).then(() => {
                    this.getList();
                    this.msgSuccess("删除成功");
                }).catch(function () { });
            },
            /** 导出按钮操作 */
            handleExport() {
                const queryParams = this.queryParams;
                this.$confirm('是否确认导出所有批次物料产品关系追溯数据项?', "警告", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(function () {
                    return exportBarcodeMaterial(queryParams);
                }).then(response => {
                    this.download(response.msg);
                }).catch(function () { });
            }
        }
    };
</script>